<?php
// +----------------------------------------------------------------------
// | Iecms [ Make the most exquisite CMS ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009~2019 http://www.iemoo.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: iemoo <i@iemoo.com>
// +----------------------------------------------------------------------
// | DateTime: 2019/11/19 11:08
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\common\model\Files as FilesModel;
use app\common\model\ArchivesFiles as ArchivesFilesModel;
use app\common\model\PageFiles as PageFilesModel;


use app\admin\validate\Files as FilesValidate;
use think\exception\ValidateException;

use think\facade\Request;
use think\facade\View;
use think\facade\Config;

/**
 * 文件管理
 * Class AdminUser
 * @package app\admin\controller
 */
class Files extends Base
{
 
    /**
     * 文件管理
     * @return mixed
     */
    public function index()
    {
        $files_list = FilesModel::order('id desc')->paginate(10);
		View::assign('list',$files_list);

		
		return View::fetch('index');
    }

    /**
     * 添加广告
     * @return mixed
     */
    public function add()
    {

		$purpose = ['system'=>'系统','adv'=>'广告','links'=>'链接','avatar'=>'头像','archives'=>'文档','page'=>'单页','fragment'=>'碎片','editor'=>'内容'];
		View::assign('purpose',$purpose);

		return View::fetch('add');
    }



    /**
     * 文件管理 - 图片
     * @return mixed
     */
    public function image()
    {

		$files_list = FilesModel::where('ext','in',['jpeg','png','jpg','gif'])->paginate(24);
		
		foreach($files_list as $key => $val){
			$files_list[$key]['archives_count'] = ArchivesFilesModel::where('files_id',$val['id'])->count();
			$files_list[$key]['page_count'] = PageFilesModel::where('files_id',$val['id'])->count();
		}

		View::assign('list',$files_list);		
		return View::fetch('image');
    }

    /**
     * 文件管理 - 媒体
     * @return mixed
     */
    public function media()
    {
        $files_list = FilesModel::where('ext','in',['mp3','mp4','avi','wav'])->paginate(10);
		
		View::assign('list',$files_list);		
		return View::fetch('media');
    }
    /**
     * 文件管理 - 压缩包
     * @return mixed
     */
    public function zips()
    {
        $files_list = FilesModel::where('ext','in',['zip','rar'])->paginate(10);
		
		foreach($files_list as $key => $val){
			$files_list[$key]['count'] = ArchivesFilesModel::where('files_id',$val['id'])->count();
		}

		View::assign('list',$files_list);		
		return View::fetch('index');
    }

    /**
     * 文件管理 - 文档
     * @return mixed
     */
    public function words()
    {
        $files_list = FilesModel::where('ext','in',['txt','pdf','docx','xlsx','doc','xls'])->paginate(10);
		
		foreach($files_list as $key => $val){
			$files_list[$key]['count'] = ArchivesFilesModel::where('files_id',$val['id'])->count();
		}

		View::assign('list',$files_list);		
		return View::fetch('index');
    }


    /**
     * 删除文件
     * @param $id
     */
    public function delete($id)
    {
		$file = FilesModel::find($id);
		$url = $file->url;
        if ($file->delete()) {
			@unlink('.'.$url);
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}